package cc.autoapi.pucong.auto.flow.persistence.repository.log;


import cc.autoapi.pucong.auto.flow.persistence.po.log.ApiInstancePo;
import cc.autoapi.pucong.auto.flow.persistence.utils.jdbc.DatabaseUtils;
import cc.autoapi.pucong.auto.flow.persistence.utils.jdbc.ExeRunSqlResultPage;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author pucong
 * @description 项目api实例 service 实现类
 * @create 2024-10-07
 */
@Service
public class ApiInstanceServiceImpl implements ApiInstanceService {

    @Override
    public void appendApiLog(String apiInstanceId, String string) {
        ApiInstancePo apiInstancePo = getById(apiInstanceId);
        if (apiInstancePo != null) {
            String logContent = apiInstancePo.getLogContent();
            if (logContent == null) {
                logContent = string;
            } else {
                logContent = logContent + "\n" + string;
            }
            ApiInstancePo update = new ApiInstancePo();
            update.setUuid(apiInstancePo.getUuid());
            update.setLogContent(logContent);
            updateById(update);
        }
    }

    @Override
    public void deleteByApiId(List<String> apiIds) {
        if (apiIds.size() == 0) {
            return;
        }
        removeBatchByApiIds(apiIds);
    }

    private void removeBatchByApiIds(List<String> apiIds) {
        if (apiIds == null || apiIds.size() < 1) {
            return;
        }
        StringBuffer sqlBf = new StringBuffer();
        sqlBf.append("delete from auto_flow_api_instance where api_id in (");
        for (int i = 0; i < apiIds.size(); i++) {
            sqlBf.append("?,");
        }
        sqlBf.deleteCharAt(sqlBf.length() - 1);
        sqlBf.append(")");
        List<Object> params = new ArrayList<>();
        params.addAll(apiIds);

        DatabaseUtils.execSql(params, sqlBf.toString(), Integer.class);
    }

    @Override
    public ApiInstancePo getById(String instanceId) {
        List<Object> params = new ArrayList<>();
        StringBuffer sqlBf = new StringBuffer();
        sqlBf.append("select * from auto_flow_api_instance where 1=1 and uuid = ? ");
        params.add(instanceId);
        ApiInstancePo apiInstancePo = DatabaseUtils.execQueryOneSql(params, sqlBf.toString(), ApiInstancePo.class);
        return apiInstancePo;
    }

    @Override
    public void save(ApiInstancePo apiInstancePo) {
        DatabaseUtils.saveAuto("auto_flow_api_instance", apiInstancePo);
    }

    @Override
    public void updateById(ApiInstancePo updateId) {
        DatabaseUtils.updateAutoById("auto_flow_api_instance", updateId);
    }

    @Override
    public Boolean removeBatchByIds(List<String> ids) {
        if (ids == null || ids.size() < 1) {
            return false;
        }
        StringBuffer sqlBf = new StringBuffer();
        sqlBf.append("delete from auto_flow_api_instance where uuid in (");
        for (int i = 0; i < ids.size(); i++) {
            sqlBf.append("?,");
        }
        sqlBf.deleteCharAt(sqlBf.length() - 1);
        sqlBf.append(")");
        List<Object> params = new ArrayList<>();
        params.addAll(ids);

        DatabaseUtils.execSql(params, sqlBf.toString(), Integer.class);
        return true;
    }

    @Override
    public ExeRunSqlResultPage<ApiInstancePo> page(int pageNum, int pageSize, String apiId, Date exeStartTime, Date exeEndTime, Integer exeStatus) {
        List<Object> params = new ArrayList<>();
        StringBuffer sqlBf = new StringBuffer();
        sqlBf.append("select * from auto_flow_api_instance where 1=1 ");
        if (StringUtils.isNotBlank(apiId)) {
            sqlBf.append(" and api_id = ? ");
            params.add(apiId);
        }
        if (exeStartTime != null) {
            sqlBf.append(" and exe_start_time >= ? ");
            params.add(exeStartTime);
        }
        if (exeEndTime != null) {
            sqlBf.append(" and exe_end_time <= ? ");
            params.add(exeEndTime);
        }
        if (exeStatus != null) {
            sqlBf.append(" and exe_status = ? ");
            params.add(exeStatus);
        }
        sqlBf.append(" order by create_time desc ");
        ExeRunSqlResultPage<ApiInstancePo> page = DatabaseUtils.runSQLByPage(pageNum, pageSize, sqlBf.toString(), params, ApiInstancePo.class);
        return page;
    }
}
